package solution._0040._0041;

import common.Utils;

/**
 * Created by chen_snow
 * Date: 2021/5/28 23:21
 * Description: 缺失的第一个正数
 */
public class Solution {

    public static void main(String[] args) {
        firstMissingPositive(new int[]{1, 1});
    }

    public static int firstMissingPositive(int[] nums) {
        int length = nums.length;
        for (int i = 0; i < length; i++) {
            if (nums[i] <= 0) {
                nums[i] = length + 1;
            }
        }

        Utils.print(nums);

        for (int i = 0; i < length; i++) {
            if (Math.abs(nums[i]) < length + 1) {
                nums[Math.abs(nums[i]) - 1] = -Math.abs(nums[Math.abs(nums[i]) - 1]);
            }
        }

        Utils.print(nums);

        for (int i = 0; i < length; i++) {
            if (nums[i] > 0) {
                return i + 1;
            }
        }
        return length + 1;
    }
}
